// user/pages/healthRecords/healthRecords.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userHealth: {},
    showSheet: false, //选择器弹窗
    selectStr: '', //选择弹窗绑定的属性
    actions: [],
    sheetList: [{
        name: '有'
      },
      {
        name: '无'
      }
    ],
  },

  getUserRecord() {
    var url = app.globalData.url + "user/getHealthRecord",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userId: app.globalData.userId,
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          var datas = res.data
          if (datas != null) {
            datas.isDrink = datas.isDrink + ''
            datas.isMotion = datas.isMotion + ''
            datas.isSmoke = datas.isSmoke + ''
            that.setData({
              userHealth: datas,
            })
          }
          console.log(that.data);
        }
      },
      err => {
        console.log(err);
      })
  },
  // 保存
  saveHandle() {
    var userHealth = this.data.userHealth;

    var url = app.globalData.url + "user/addHealthRecords",
      that = this;
    wx.showLoading({
      title: '',
    })
    console.log(userHealth);
    var obj = {
      userId: app.globalData.userId,
      userHeight: userHealth.userHeight,
      userWeight: userHealth.userWeight,
      pastMedical: userHealth.pastMedical,
      allergy: userHealth.allergy,
      familyHistory: userHealth.familyHistory,
      isMotion: userHealth.isMotion,
      isSmoke: userHealth.isSmoke,
      isDrink: userHealth.isDrink,
    }

    
    if (userHealth.recordId) {
      obj.recordId = userHealth.recordId
    }

    console.log(obj);

    app.wxRequest(url, obj, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '提交成功',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {}
            }
          })
          console.log(that.data);
        }
      },
      err => {
        console.log(err);
      })
  },


  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
  },




  // 选择框
  onChange(event) {
    var str = event.currentTarget.dataset.flag
    this.setData({
      [str]: event.detail
    });
    console.log(this.data.userHealth);
  },
  // 显示选择弹窗
  showSelect(e) {
    console.log(e);
    var str = e.currentTarget.dataset.flag
    this.setData({
      actions: this.data.sheetList,
      selectStr: str,
      showSheet: true
    })

  },

  // 选择弹窗  确认
  onSelect(e) {
    console.log(e);

    this.setData({
      [this.data.selectStr]: e.detail.name,
      showSheet: false
    })

    console.log(this.data);
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserRecord()
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})